package xyf.p09.bfs_dfs;

/**
 * ClassName: M0810_FloodFill
 * Description:
 * Author: xyf
 * Date: 9/9/22 9:18 AM
 * Version: 1.0
 **/
public class M0810_FloodFill
{
    public int[][] floodFill(int[][] image, int sr, int sc, int newColor)
    {
        boolean[][] visited = new boolean[image.length][image[0].length];
        dfs(sr, sc, image, newColor, visited, image[sr][sc]);
        return image;
    }

    public void dfs(int row, int col, int[][] image, int newColor, boolean[][] visited, int oldColor)
    {
        if (row < 0 || col < 0 || row > image.length - 1 || col > image[0].length - 1
                || visited[row][col] || image[row][col] != oldColor)
        {
            return;
        }
        image[row][col] = newColor;
        visited[row][col] = true;

        dfs(row - 1, col, image, newColor, visited, oldColor);
        dfs(row + 1, col, image, newColor, visited, oldColor);
        dfs(row, col - 1, image, newColor, visited, oldColor);
        dfs(row, col + 1, image, newColor, visited, oldColor);
    }
}
